package com.lxt.cloud.testhystrix.controller;

import com.lxt.cloud.feign.TestHystrixFeigh;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


@RestController
@DefaultProperties(defaultFallback = "paymentInfo_TimeoutHandler")
public class HystrixController {

        @Autowired
        private TestHystrixFeigh testHystrixFeigh;

        @Value("${server.port}")
        private String serverPost;

        @GetMapping("/consumer/hystrix/OK/{id}")
        //这里没有写@HystrixCommand注解，但由于对feign接口做了服务降级类TestHystrixFeighFallBack，
        // 所以也可以完成服务降级。例如testcloud1宕机
        public String paymentInfo_OK(@PathVariable("id") Long id){
            return testHystrixFeigh.paymentInfo_OK(id);
        }

//        @GetMapping("/consumer/hystrix/Timeout/{id}")
        //自定义fallback
//        @HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler", commandProperties = {
//                @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
//        })
        //只加注解，就会用到@DefaultProperties中的fallback方法
        @GetMapping("/consumer/hystrix/Timeout/{id}")
        @HystrixCommand
        public String paymentInfo_TimeOut(@PathVariable("id") Long id){
            int x = 1/0;
            return testHystrixFeigh.paymentInfo_TimeOut(id);
        }

        public String paymentInfo_TimeoutHandler(@PathVariable("id") Long id){
            return "我是消费者8005，对方支付系统繁忙，请10秒后再试，o(╥﹏╥)o";
        }

        public String paymentInfo_TimeoutHandler(){
            return "我是消费者8005，对方支付系统报错，调用公用的fallback方法，请10秒后再试，o(╥﹏╥)o";
        }
    }

